package com.wjb.xietong.ota.tab3;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.wjb.xietong.R;
import com.wjb.xietong.control.WJBControl;
import com.wjb.xietong.datamanager.LoginUserDataManager;
import com.wjb.xietong.datamanager.WJBDataManager;
import com.wjb.xietong.ota.BaseFM;
import com.wjb.xietong.ota.task.OTATaskDetail;
import com.wjb.xietong.server.IRequestResultListener;
import com.wjb.xietong.server.notify.GetNotifyParam;
import com.wjb.xietong.server.notify.GetNotifyResponse.NotifyInfo;
import com.wjb.xietong.util.EnumDefine.Login_User_Info;
import com.wjb.xietong.view.PullToRefreshView;
import com.wjb.xietong.view.PullToRefreshView.OnFooterRefreshListener;
import com.wjb.xietong.view.PullToRefreshView.OnHeaderRefreshListener;

public class FMTab3 extends BaseFM implements OnHeaderRefreshListener, OnFooterRefreshListener {
	private List<NotifyInfo> notifyInfoList = new ArrayList<NotifyInfo>();
	private ListView mListView;
	private ListAdapter mAdapter;
	
	private PullToRefreshView mPullToRefreshView;
	private final int PAGE_INDEX = 1;
	private final int NUM_EVERY_PAGE = 10;
	private int mPageIndex = 1;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mPageIndex = 1;
		View view = super.onCreateView(inflater, container, savedInstanceState);
		setContentField(inflater.inflate(R.layout.fm_tab3, null));
		setTopBarAndAction(getString(R.string.STR_TAB_03), null, null);
		return view;
	}
	
	
	@Override
	protected void findView(View v) {
		mPullToRefreshView = (PullToRefreshView) v.findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this, PAGE_INDEX);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		
		mListView = (ListView)v.findViewById(R.id.id_listview);
		mAdapter = new ListAdapter(getActivity());
		mListView.setAdapter(mAdapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(), OTATaskDetail.class);
				intent.putExtra("from_notice", true);
				intent.putExtra("taskId", notifyInfoList.get(arg2).getTaskId());
				intent.putExtra("companyId", notifyInfoList.get(arg2).getCompanyId());
				intent.putExtra("projectId", notifyInfoList.get(arg2).getProjectId());
				startActivity(intent);
			}
		});
		
		requestGetNotifyInfoList(true, mPageIndex, NUM_EVERY_PAGE, 0);
	}
	
	/**
	 * 请求获取通知列表
	 * 
	 *  @param readFlag（0是未读，1是已读，未读和已读也要像任务那样分两页）
	 */
	private void requestGetNotifyInfoList(final boolean isRefresh, final int beginRow, final int endRow, final int readFlag){
		showProgressDialog();
		GetNotifyParam param = new GetNotifyParam();
		param.setPageIndex(beginRow);
		param.setPageSize(endRow);
		param.setReadFlag(readFlag);
		param.setUserId((Long)LoginUserDataManager.getLoginUserInfo(Login_User_Info.ID));
		WJBControl.requestGetNotify(Calendar.getInstance().getTimeInMillis(), param, new IRequestResultListener() {
			
			@Override
			public void requestSuccess(long requestUniqueID) {
				hideProgressDialog();
				if(isRefresh){
					notifyInfoList.clear();
				}
				if(null != WJBDataManager.getNotifyResponseInfo().getNotifyInfoList()){
					notifyInfoList.addAll(WJBDataManager.getNotifyResponseInfo().getNotifyInfoList());
				}
				mAdapter.notifyDataSetChanged();
			}
			
			@Override
			public void requestFaield(long requestUniqueID, String errCode,
					String errMsg) {
				hideProgressDialog();
			}
		});
	}

	@Override
	protected void setOnClickEvent() {
		// TODO Auto-generated method stub
		super.setOnClickEvent();
	}

	public class ListAdapter extends BaseAdapter{

		private LayoutInflater mInflater; 
        
        public ListAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }
        
		@Override
		public int getCount() {
			return notifyInfoList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return notifyInfoList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_notice_list,
						null);
				holder = new ViewHolder();
				/* 得到各个控件的对象 */
				holder.txtA = (TextView) convertView.findViewById(R.id.txt_a);
				holder.txtB1 = (TextView) convertView.findViewById(R.id.txt_b1);

				convertView.setTag(holder); // 绑定ViewHolder对象
			} else {
				holder = (ViewHolder) convertView.getTag(); // 取出ViewHolder对象
			}

			holder.txtB1.setText(""+notifyInfoList.get(position).getNotifyInfo());
			holder.txtA.setText(""+notifyInfoList.get(position).getGmtCreate());
			return convertView;
		}
		
		/*存放控件 的ViewHolder*/
		public final class ViewHolder {
			public TextView txtA;
			public TextView txtB1;
		}
	}
	
	@Override
	public void onFooterRefresh(PullToRefreshView view, final int pageIndex) {
		mPullToRefreshView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mPullToRefreshView.onFooterRefreshComplete();
				mPageIndex = pageIndex;
				requestGetNotifyInfoList(false, pageIndex, NUM_EVERY_PAGE, 0);
			}
		}, 1000);
	}
	
	@Override
	public void onHeaderRefresh(PullToRefreshView view, final int pageIndex) {
		mPullToRefreshView.postDelayed(new Runnable() {
	
			@Override
			public void run() {
				mPullToRefreshView.onHeaderRefreshComplete();
				mPageIndex = pageIndex;
				requestGetNotifyInfoList(true, pageIndex, NUM_EVERY_PAGE, 0);
			}
		}, 1000);
	}
}